package com.ruoyi.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.vo.BatchGoods;
import com.ruoyi.business.domain.vo.GoodTreeDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BizGoods;
import com.ruoyi.business.service.IBizGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品列表Controller
 * 
 * @author ruoyi
 * @date 2024-05-22
 */
@RestController
@RequestMapping("/business/goods")
public class BizGoodsController extends BaseController
{
    @Autowired
    private IBizGoodsService bizGoodsService;

    /**
     * 查询商品列表列表
     */
    @PreAuthorize("@ss.hasPermi('business:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizGoods bizGoods)
    {
        startPage();
        List<BizGoods> list = bizGoodsService.selectBizGoodsList(bizGoods);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('business:goods:list')")
    @PostMapping("/goodTree")
    public AjaxResult goodTree(@RequestBody GoodTreeDto goodTreeDto)
    {
        return success(bizGoodsService.goodTree(goodTreeDto));
    }
    /**
     * 导出商品列表列表
     */
    @PreAuthorize("@ss.hasPermi('business:goods:export')")
    @Log(title = "商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizGoods bizGoods)
    {
        List<BizGoods> list = bizGoodsService.selectBizGoodsList(bizGoods);
        ExcelUtil<BizGoods> util = new ExcelUtil<BizGoods>(BizGoods.class);
        util.exportExcel(response, list, "商品列表数据");
    }

    /**
     * 获取商品列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:goods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return success(bizGoodsService.selectBizGoodsByGoodsId(goodsId));
    }

    /**
     * 新增商品列表
     */
    @PreAuthorize("@ss.hasPermi('business:goods:add')")
    @Log(title = "商品列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizGoods bizGoods)
    {
        bizGoods.setTenantId(getUserId());
        bizGoods.setCreateBy(getUsername());
        return toAjax(bizGoodsService.insertBizGoods(bizGoods));
    }

    /**
     * 修改商品列表
     */
    @PreAuthorize("@ss.hasPermi('business:goods:edit')")
    @Log(title = "商品列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizGoods bizGoods)
    {
        bizGoods.setUpdateBy(getUsername());
        return toAjax(bizGoodsService.updateBizGoods(bizGoods));
    }
    @PreAuthorize("@ss.hasPermi('business:goods:batchUpdate')")
    @Log(title = "批量修改", businessType = BusinessType.UPDATE)
    @PostMapping("/batchGoods")
    public AjaxResult batchGoods(@RequestBody BatchGoods batchGoods)
    {
        batchGoods.setUpdateBy(getUsername());
        return toAjax(bizGoodsService.batchGoods(batchGoods));
    }

    @PreAuthorize("@ss.hasPermi('business:goods:batchAudit')")
    @Log(title = "批量审核", businessType = BusinessType.UPDATE)
    @PutMapping("/batchAudit")
    public AjaxResult batchAudit()
    {
        return toAjax(bizGoodsService.batchAudit(getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('business:goods:edit')")
    @Log(title = "状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody BizGoods bizGoods)
    {
        bizGoods.setUpdateBy(getUsername());
        return toAjax(bizGoodsService.updateBizGoods(bizGoods));
    }


    /**
     * 删除商品列表
     */
    @PreAuthorize("@ss.hasPermi('business:goods:remove')")
    @Log(title = "商品列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(bizGoodsService.deleteBizGoodsByGoodsIds(goodsIds));
    }
}
